﻿(function(){

    $(function(){
        var personService = abp.services.app.person;
        var _$modal = $('#PersonCreateModal');
        var _$form = _$modal.find('form');
        //刷新
        $('#RefreshButton').click(function () {
            refreshPersonList();
        });

        //删除
        $('.delete-person').click(function () {
            var personId = $(this).attr("data-person-id");
            var personName = $(this).attr('data-person-name');

            deletePerson(personId, personName);
        });

        //编辑
        $('.edit-person').click(function (e) {
            var personId = $(this).attr("data-person-id");

            e.preventDefault();

            personService.getPersonForEdit({ id: personId }).done(function (data) {
                $("input[name=Id]").val(data.person.id);
                $("input[name=Name]").val(data.person.name).parent().addClass("focused");
                $("input[name=EmailAddress]").val(data.person.emailAddress).parent().addClass("focused");
                $("input[name=PhoneNumber]").val(data.person.phoneNumbers[0].number).parent().addClass("focused");
                $("select[name=PhoneNumberType]").selectpicker('val',data.person.phoneNumbers[0].type).parent().addClass("focused");
                $("input[name=Address]").val(data.person.address).parent().addClass("focused");
            });
        });

        //增加
        _$form.find('button[type="submit"]').click(function (e) {
            e.preventDefault();

            if (!_$form.valid()) {
                return;
            }

            var personEditDto = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js

            personEditDto.PhoneNumbers=[];
            var phoneNumber = {};
            phoneNumber.Type = personEditDto.PhoneNumberType;
            phoneNumber.Number = personEditDto.PhoneNumber;
            personEditDto.PhoneNumbers.push(phoneNumber);
         
            abp.ui.setBusy(_$modal);
            personService.createOrUpdatePerson({ personEditDto }).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see new user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });

        _$modal.on('hide.bs.modal', function () {
            _$form[0].reset();
        });

        _$modal.on('shown.bs.modal', function () {
            _$modal.find('input:not([type=hidden]):first').focus();
        });

        //刷新方法
        function refreshPersonList() {
            location.reload(true); //reload page to see new user!
        }

        //删除方法
        function deletePerson(personId, personName) {
            abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'MPACorePhoneBook'), personName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        personService.deletePerson({ id : personId }).done(function () {
                            refreshPersonList();
                        });
                    }
                }
            );
        }

    });


})();